﻿//www.codeOften.com
//Senthil Maruthaiappan - senips (at) gmail.com
//see license on http://www.codeplex.com/Jinglebell

using System;
using System.Windows.Forms;
using CodeOften.Container;
using JingleBell.Interface;
using JingleBell.Library;

namespace JingleBell
{
    [InstanceBehaviour(InstanceBehaviourType.Singleton)]
    public partial class MainForm : Form, IProcessListener, IMainForm
    {
        private CommandItems _activeCommands;

        private bool databinding;

        public MainForm()
        {
            InitializeComponent();
        }

        public IServices Services { get; set; }

        #region IMainForm Members

        public object Instance
        {
            get { return this; }
        }

        #endregion

        #region IProcessListener Members

        public void ProcessResultOut(string text)
        {
            AppendText(text);
        }

        public void ProcessFail(Exception excp)
        {
            AppendText(excp.InnerException.ToString());
        }

        public void ProcessCancelled()
        {
            AppendText("Cancelled.");
        }

        public void ProcessCompleted()
        {
            AppendText("Completed");
        }


        public void ProcessInvalidCommand(Exception excp)
        {
            AppendText(excp.InnerException.ToString());
        }

        #endregion

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            _activeCommands.CommandList.Add(new CommandItem {CommandName = "(New)"});
            CheckCheckBoxes();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            BindList();
        }

        private void BindList()
        {
            listBoxCommands.BeginUpdate();
            databinding = true;
            try
            {
                _activeCommands = CommandItems.LoadSettings(CommandHelper.SettingsFile);
                listBoxCommands.DataSource = _activeCommands.CommandList;
                CheckCheckBoxes();
            }
            finally
            {
                databinding = false;
                listBoxCommands.EndUpdate();
            }
        }

        private void CheckCheckBoxes()
        {
            for (int i = 0; i < _activeCommands.CommandList.Count; i++)
                listBoxCommands.SetItemChecked(i, ((CommandItem) _activeCommands.CommandList[i]).Active);
        }


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _activeCommands.Save();
        }

        private void listBoxCommands_SelectedIndexChanged(object sender, EventArgs e)
        {
            var commandItem = listBoxCommands.SelectedItem as CommandItem;
            if (commandItem != null)
            {
                propertyGridCommandItem.SelectedObject = commandItem;
            }
        }

        private void toolStripButtonRunSelected_Click(object sender, EventArgs e)
        {
            richTextBoxOutput.Clear();


            var processRunner = Services.Get<IProcessRunner>(this, this);
            foreach (CommandItem commandItem in listBoxCommands.CheckedItems)
                processRunner.Queue.Enqueue(commandItem);
            processRunner.Run();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            var commandItem = listBoxCommands.SelectedItem as CommandItem;
            if (commandItem != null)
            {
                _activeCommands.CommandList.Remove(commandItem);
                listBoxCommands_SelectedIndexChanged(sender, e);
            }
            CheckCheckBoxes();
        }

        private void AppendText(string text)
        {
            richTextBoxOutput.Focus();
            richTextBoxOutput.AppendText(text);
            richTextBoxOutput.AppendText("\n");
        }

        private void propertyGridCommandItem_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            ChangeCheckState();
        }

        private void ChangeCheckState()
        {
            var commandItem = listBoxCommands.SelectedItem as CommandItem;
            if (commandItem != null)
            {
                int idx = listBoxCommands.Items.IndexOf(commandItem);
                if (idx != -1)
                    listBoxCommands.SetItemChecked(idx, commandItem.Active);
            }
        }

        private void listBoxCommands_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (databinding)
                return;

            var commandItem = listBoxCommands.SelectedItem as CommandItem;
            if (commandItem != null)
            {
                commandItem.Active = (e.NewValue == CheckState.Checked);
            }
        }
    }
}